package com.ymttest.business.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.apache.http.client.ClientProtocolException;
import org.springframework.data.mongodb.core.aggregation.VariableOperators.Map;

import com.google.gson.JsonObject;
import com.ymatou.iapi.counter.parameter.*;
import com.ymatou.iapi.counter.service.*;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/************************************************************************************
 * @File name :CounterCallService.java
 * @Author : zhouyi
 * @Date : 2015年7月9日
 * @Copyright : 洋码头
 ************************************************************************************/
public class CounterCallService {
	/**
	 * 计数服务 setSingle
	 * 
	 * @param appid
	 *            系统ID*
	 * @param TargetId
	 *            业务聚合键
	 * @param action
	 *            1.增加计数2.减少计数3.设置计数4.计数清零
	 * @param value
	 *            计数值
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public void setSingle(String appid, String TargetId, int action, int value) throws ClientProtocolException, IOException {
		setSingleBean ssb = new setSingleBean();
		setSingleCall ssc = new setSingleCall();
		ssb.setAppId(appid);
		ssb.setTargetId(TargetId);
		ssb.setCounter("c1");
		ssb.setAction(action);
		ssb.setValue(value);
		ssc.setData(false, ssb);
		ssc.callService();
		Logger.debug("setSingleCall:" + ssc.getEntityString());
		Logger.debug("setSingleCall return:" + ssc.getReturnData());
	}

	public void setSingle(String appid, String counter, String TargetId, int action, int value) throws ClientProtocolException, IOException {
		setSingleBean ssb = new setSingleBean();
		setSingleCall ssc = new setSingleCall();
		ssb.setAppId(appid);
		ssb.setTargetId(TargetId);
		ssb.setCounter(counter);
		ssb.setAction(action);
		ssb.setValue(value);
		ssc.setData(false, ssb);
		ssc.callService();
		Logger.debug("setSingleCall:" + ssc.getEntityString());
		Logger.debug("setSingleCall return:" + ssc.getReturnData());
	}

	/**
	 * 计数服务 setGroup
	 * 
	 * @param appid
	 *            系统ID
	 * @param TargetId
	 *            业务聚合键
	 * @param action
	 *            1.增加计数2.减少计数3.设置计数4.计数清零
	 * @param value
	 *            计数值
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public void setGroup(String appid, String Counter, String TargetId, int action, int value) throws ClientProtocolException, IOException {
		setGroupBean ssb = new setGroupBean();
		setGroupCall ssc = new setGroupCall();
		ssb.setAppId(appid);
		ssb.setTargetId(TargetId);
		ssb.setCounter(Counter);
		ssb.setAction(action);
		ssb.setValue(value);
		ssc.setData(false, ssb);
		ssc.callService();
		Logger.debug("setGroupCall:" + ssc.getEntityString());
		Logger.debug("setGroupCall return:" + ssc.getReturnData());
	}

	/**
	 * 创建natural计数器
	 * 
	 * @param appid
	 * @param Counter
	 * @param TargetId
	 * @param action
	 * @param value
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public void setNatural(String appid, String Counter, String TargetId, int action, int value) throws ClientProtocolException, IOException {
		SetNaturalBean setNaturalBean = new SetNaturalBean();
		SetNaturalCall setNaturalCall = new SetNaturalCall();
		setNaturalBean.setAppId(appid);
		setNaturalBean.setAction(action);
		setNaturalBean.setCounter(Counter);
		String DataTime = YMTDateUtil.getBeforeOrNextDay(0, "yyyy-MM-dd'T'HH:mm:ss");
		setNaturalBean.setDataTime(DataTime);
		setNaturalBean.setTargetId(TargetId);
		setNaturalBean.setValue(value);
		setNaturalCall.setData(false, setNaturalBean);
		setNaturalCall.callService();
		Logger.debug(setNaturalCall.getEntityString());
		Logger.debug(setNaturalCall.getReturnData());

	}

	/**
	 * 由于本地和服务器时间差问题,获取了最近前后5分钟的数据并累加counter
	 * 
	 * @param appid
	 * @param Counter
	 * @param TargetId
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static Integer getRegisterRiskSecond(String targetIds) throws ClientProtocolException, IOException {
		GetNaturalListBean getNaturalBean = new GetNaturalListBean();
		GetNaturalListCall getNaturalCall = new GetNaturalListCall();
		getNaturalBean.setAppId("usersafe");
		getNaturalBean.setCounter("RegisterRiskMinute");
		String stime = YMTDateUtil.getBeforeOrNextSecond(-300, "yyyy-MM-dd'T'HH:mm:ss");
		String etime = YMTDateUtil.getBeforeOrNextSecond(300, "yyyy-MM-dd'T'HH:mm:ss");
		getNaturalBean.setStartTime(stime);
		getNaturalBean.setEndTime(etime);
		getNaturalBean.setTargetId(targetIds);
		getNaturalCall.setData(false,getNaturalBean);
		getNaturalCall.callService();
		String data = getNaturalCall.getJsonObject().get("Data").toString();
		HashMap m = (HashMap) FormatUtil.GSONToObject(data, HashMap.class);
		Set s = m.keySet();
		Double allcount = 0.0;
		for (Object i : s) {
			allcount += Double.valueOf(m.get(i.toString()).toString());
		}
		return allcount.intValue();
	}
	public static Integer getSingleValue(String appid,String TargetId,String Counter) throws ClientProtocolException, IOException{
		getSingleBean  ssb=new getSingleBean();
		getSingleCall ssc=new getSingleCall();
		ssb.setAppId(appid);
		ssb.setTargetId(TargetId);
		ssb.setCounter(Counter);
		ssc.setData(false,ssb);
		ssc.callService();
		return ssc.getData();
	}
	
	/**
	 * 根据直播间id获取计数器
	 * @param appid
	 * @param TargetId
	 * @param Counter
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws InterruptedException 
	 */
	public static Integer getSingleValueByLiveId(Integer liveId) throws ClientProtocolException, IOException, InterruptedException{
		Thread.sleep(1000);
		getSingleBean  ssb=new getSingleBean();
		getSingleCall ssc=new getSingleCall();
		ssb.setAppId("liveSeller");
		ssb.setTargetId(""+liveId);
		ssb.setCounter("productSyncCounter");
		ssc.setData(ssb);
		ssc.callService();
		return ssc.getData();
	}
	public static Integer getNatural(String appid,String TargetId,String Counter) throws ClientProtocolException, IOException{
		GetNaturalBean  ssb=new GetNaturalBean();
		GetNaturalCall ssc=new GetNaturalCall();
		String DataTime = YMTDateUtil.getBeforeOrNextSecond(0,"yyyy-MM-dd'T'HH:mm:ss");
		ssb.setTime(DataTime);
		ssb.setAppId(appid);
		ssb.setTargetId(TargetId);
		ssb.setCounter(Counter);
		ssc.setData(false,ssb);
		ssc.callService();
		return ssc.getData();
	}
}
